package cn.hamster3.mc.plugin.core.bukkit.page.handler;

import cn.hamster3.mc.plugin.core.bukkit.page.ButtonGroup;
import cn.hamster3.mc.plugin.core.bukkit.page.PageConfig;
import cn.hamster3.mc.plugin.core.bukkit.page.PageManager;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * GUI 处理类
 */
@SuppressWarnings("unused")
public abstract class PageHandler implements InventoryHolder {
    private final PageConfig pageConfig;
    private final HumanEntity player;
    protected Inventory inventory;

    public PageHandler(@NotNull HumanEntity player) {
        pageConfig = PageManager.getPageConfig(getClass());
        this.player = player;
    }

    public PageHandler(@NotNull PageConfig pageConfig, @NotNull HumanEntity player) {
        this.pageConfig = pageConfig;
        this.player = player;
    }

    public abstract void initPage();

    public void onOpen(@NotNull InventoryOpenEvent event) {
    }

    public void onClick(@NotNull InventoryClickEvent event) {
        if (event.getSlot() == event.getRawSlot()) {
            return;
        }
        event.setCancelled(true);
    }

    public void onClickInside(@NotNull InventoryClickEvent event) {
        event.setCancelled(true);
    }

    public void onClickInside(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) {
    }

    public void onPlayButtonSound(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) {
        if (!(player instanceof Player)) {
            return;
        }
        PageConfig config = getPageConfig();
        String buttonName = getButtonGroup().getButtonName(index);
        Sound sound = config.getButtonSound(buttonName);
        if (sound == null) {
            switch (buttonName) {
                case "empty":
                case "barrier":
                    break;
                default:
                    sound = config.getButtonSound("default");
            }
        }
        if (sound == null) {
            return;
        }
        ((Player) player).playSound(player.getLocation(), sound, 1, 1);
    }

    public void onDrag(@NotNull InventoryDragEvent event) {
    }

    public void onDragInside(@NotNull InventoryDragEvent event) {
        event.setCancelled(true);
    }

    public void onClose(@NotNull InventoryCloseEvent event) {
    }

    public void show() {
        show(true);
    }

    public void show(boolean init) {
        if (init) {
            initPage();
        }
        Bukkit.getScheduler().runTask(pageConfig.getPlugin(), () -> player.openInventory(getInventory()));
    }

    public void close() {
        Bukkit.getScheduler().runTask(pageConfig.getPlugin(), player::closeInventory);
    }

    @NotNull
    public PageConfig getPageConfig() {
        return pageConfig;
    }

    @NotNull
    public HumanEntity getPlayer() {
        return player;
    }

    @NotNull
    public ButtonGroup getButtonGroup() {
        return getPageConfig().getButtonGroup("default");
    }

    @NotNull
    public String getTitle() {
        String title = pageConfig.getTitle();
        if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
            title = PlaceholderAPI.setPlaceholders((Player) player, title);
        }
        for (Map.Entry<String, String> entry : getPageVariables().entrySet()) {
            title = title.replace(entry.getKey(), entry.getValue());
        }
        return title;
    }

    @NotNull
    @Override
    public Inventory getInventory() {
        return inventory;
    }

    @NotNull
    public HashMap<String, String> getPageVariables() {
        return new HashMap<>();
    }
}
